#include "DATA_SAVE_module/DATA_SAVE.hpp"

#ifdef DBG_TAG
#undef DBG_TAG
#endif
#ifdef DBG_LVL
#undef DBG_LVL
#endif

#define DBG_TAG     "data_save_device"
#define DBG_LVL     DBG_LOG

extern "C"
{
#include <rtdbg.h>
}

using namespace std;

/******************************************************************************
 *
 * @brief:     _DATA_VECTOR类实现
 *
 ******************************************************************************/

_DATA_VECTOR::_DATA_VECTOR(std::string name, data_save_type data_type)
{
    this->name = name;
    this->data_type = data_type;
    switch (data_type)
    {
    case data_save_type_uint32_t:
        this->data_buff._u32 = 0;
        break;
    case data_save_type_uint16_t:
        this->data_buff._u16 = 0;
        break;
    case data_save_type_uint8_t:
        this->data_buff._u8 = 0;
        break;
    case data_save_type_int32_t:
        this->data_buff._s32 = 0;
        break;
    case data_save_type_int16_t:
        this->data_buff._s16 = 0;
        break;
    case data_save_type_int8_t:
        this->data_buff._s8 = 0;
        break;
    case data_save_type_float_t:
        this->data_buff._f32 = 0;
        break;
    default:
        break;
    }
}

_DATA_VECTOR::_DATA_VECTOR(const _DATA_VECTOR &p)
{
    this->data_buff = p.data_buff;
    this->data_type = p.data_type;
    this->name = p.name;
}

/******************************************************************************
 *
 * @brief:     DATA_SAVE类实现
 *
 ******************************************************************************/

DATA_SAVE::DATA_SAVE(EEPROM *eeprom,
                     uint32_t config_start_page,
                     uint32_t config_page_num,
                     uint32_t short_cycle_data_start_page,
                     uint32_t short_cycle_data_page_num,
                     uint32_t long_cycle_data_start_page,
                     uint32_t long_cycle_data_page_num)
{

    _DATA_VECTOR long_data_addr("long_data_addr", data_save_type_uint32_t);
    _DATA_VECTOR short_data_index("short_data_index", data_save_type_uint16_t);

    this->eeprom = eeprom;
    short_cycle_data_vector_lock = rt_mutex_create("short_data_lock", RT_IPC_FLAG_PRIO);
    if (!short_cycle_data_vector_lock)
    {
        LOG_E("error: short_data_lock create lock fail");

        goto end;
    }
    long_cycle_data_vector_lock = rt_mutex_create("long_data_lock", RT_IPC_FLAG_PRIO);
    if (!long_cycle_data_vector_lock)
    {
        LOG_E("error: long_data_lock create lock fail");

        goto end;
    }
    this->eeprom->EEPROM_init_device();
    if (eeprom->EEPROM_get_status() != EEPROM_status_ok)
    {
        LOG_E("error: eeprom init fail");
        goto end;
    }

    this->config_start_page = config_start_page;
    this->config_page_num = config_page_num;
    this->short_cycle_data_start_page = short_cycle_data_start_page;
    this->short_cycle_data_page_num = short_cycle_data_page_num;
    this->long_cycle_data_start_page = long_cycle_data_start_page;
    this->long_cycle_data_page_num = long_cycle_data_page_num;

    if (short_cycle_data_page_num == 0 || long_cycle_data_page_num == 0 || config_page_num == 0)
    {
        LOG_E("error: page num is invalid");
        goto end;
    }

    if (config_start_page + config_page_num - 1 >= short_cycle_data_page_num)
    {
        LOG_E("error: config_page is over short_cycle_page");
        goto end;
    }

    if (short_cycle_data_start_page + short_cycle_data_page_num - 1 >= long_cycle_data_start_page)
    {
        LOG_E("error: short_cycle_page is over long_cycle_page");
        goto end;
    }

    this->short_cycle_data_bytes_length = 0;
    this->long_cycle_data_bytes_length = 0;
    this->short_cycle_mines_bytes = 0;
    this->debug_flag = 0;

    short_cycle_data_vector.insert(short_cycle_data_vector.begin(), long_data_addr);
    short_cycle_data_vector.insert(short_cycle_data_vector.begin(), short_data_index);
    short_cycle_data_bytes_length += 6;

    init_flag = data_save_status_not_init;
    return;
    end: init_flag = data_save_status_dead_error;
    return;
}

DATA_SAVE::~DATA_SAVE()
{
    if (this->short_cycle_data_vector_lock) rt_mutex_delete(short_cycle_data_vector_lock);
    if (this->long_cycle_data_vector_lock) rt_mutex_delete(long_cycle_data_vector_lock);
}

data_save_status DATA_SAVE::DATA_SAVE_factory_init() //初始化并清除所有已存在信息
{
    uint8_t *p = NULL;
    uint32_t i;
    uint32_t total_pages;
    uint32_t bytes_per_page;
    //检查是否完成实例化
    if (init_flag == data_save_status_fail)
    {
        LOG_W("warn: init flag is fail , factory init may not finish!");
        LOG_W("warn: this is normal, if eeprom is a new chip!");
    }

    bytes_per_page = eeprom->EEPROM_get_bytes_per_page();
    total_pages = eeprom->EEPROM_get_total_pages_num();
    //清除数据
    p = (uint8_t*) rt_malloc((uint32_t) bytes_per_page * 2);
    if (!p)
    {
        LOG_E("error: create blank data fail");
        goto end;
    }
    rt_memset(p, 0, bytes_per_page * 2);
    for (i = 0; i < total_pages; i += 2)
    {
        if (eeprom->EEPROM_write(i * bytes_per_page ,
                                 p,
                                 bytes_per_page * 2) != EEPROM_status_ok)
        {
            LOG_E("error: delete eeprom blank data fail");
            goto end;
        }
    }

    if (p) rt_free(p);
    init_flag = data_save_status_ok;
    LOG_I(" factory init ok");
    return data_save_status_ok;
    end: if (p) rt_free(p);
    init_flag = data_save_status_fail;
    LOG_E("error: factory init fail");
    return data_save_status_fail;
}

data_save_status DATA_SAVE::DATA_SAVE_init() //正常启动，读取并继承信息
{
    uint32_t read_addr, i;
    EEPROM_status flag1, flag2, flag3;
    uint8_t read_temp1[2] = { };
    uint8_t read_temp2[2] = { };
    uint8_t *data_buff = NULL;
    uint16_t short_index1, short_index2;
    uint32_t bytes_per_page = eeprom->EEPROM_get_bytes_per_page();
    uint32_t start_addr, end_addr;

    //检查是否完成实例化
    if (init_flag != data_save_status_not_init)
    {
        LOG_E("error: init data save should create data save ok first");
        goto end;
    }

    //从实时数据区读取数据
    start_addr = short_cycle_data_start_page * bytes_per_page;
    end_addr = start_addr + short_cycle_data_page_num * bytes_per_page;

    //在实时数据的区域刚好能存完2^16个数据时，需要减去的地址（避免首尾相接持续循环）
    if (short_cycle_data_bytes_length * 65536 == short_cycle_data_page_num * bytes_per_page)
        short_cycle_mines_bytes = short_cycle_data_bytes_length;

    end_addr -= short_cycle_mines_bytes;

    i = 0; //断点数量，正常为1，其他值为非正常
    for (read_addr = start_addr; read_addr < end_addr; read_addr += short_cycle_data_bytes_length)
    {
        flag1 = flag2 = flag3 = EEPROM_status_ok;
        flag1 = eeprom->EEPROM_read(read_addr, read_temp1, 2);
        if (read_addr + short_cycle_data_bytes_length >= end_addr)
            flag2 = eeprom->EEPROM_read(start_addr, read_temp2, 2);
        else
            flag3 = eeprom->EEPROM_read(read_addr + short_cycle_data_bytes_length, read_temp2, 2);

        //判断读取是否成功
        if (flag1 != EEPROM_status_ok || flag2 != EEPROM_status_ok || flag3 != EEPROM_status_ok)
        {
            LOG_E("error: read newest short data fail");
            goto end;
        }
        //读取成功判断数据
        short_index1 = _u8_buff_transfer_to_u16(read_temp1);
        short_index2 = _u8_buff_transfer_to_u16(read_temp2);

        //地址溢出断点判断
        if (short_index1 == 65535 && short_index2 == 0) continue;

        //断点判断
        if (short_index1 + 1 != short_index2)
        {
            now_newest_short_cycle_data_addr = read_addr;
            break;
        }

    }

    //排除算法出错
    if (read_addr >= end_addr)
    {
        now_newest_short_cycle_data_addr = start_addr;
        LOG_W(" newest can't find valid data,short index is a cycle between 0-65535");
        goto end;
    }

//读取最新数据
    data_buff = (uint8_t*) rt_malloc(short_cycle_data_bytes_length);
    if (!data_buff)
    {
        LOG_E("error: read newest data buff create fail");
        goto end;
    }
    if (eeprom->EEPROM_read(now_newest_short_cycle_data_addr, data_buff, short_cycle_data_bytes_length)
            != EEPROM_status_ok)
    {
        LOG_E("error: read newest data fail");
        goto end;
    }
    if (debug_flag)
    {
        LOG_D("printf the saved data-------------");
        for (i = 0; i < short_cycle_data_bytes_length; i++)
        {
            if (i % 10 == 0 && i != 0)
            {
                rt_kprintf("\r\n");
            }
            rt_kprintf("<%X>", data_buff[i]);
        }
        rt_kprintf("\r\n");

        LOG_D("printf the saved data end --------");
    }

    i = 0; //使用data_buff第几字节
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele_data : short_cycle_data_vector)
    {
        switch (ele_data.data_type)
        {
        case data_save_type_uint32_t:
            ele_data.data_buff._u32 = _u8_buff_transfer_to_u32(data_buff + i);
            i += 4;
            break;
        case data_save_type_uint16_t:
            ele_data.data_buff._u16 = _u8_buff_transfer_to_u16(data_buff + i);
            i += 2;
            break;
        case data_save_type_uint8_t:
            ele_data.data_buff._u8 = _u8_buff_transfer_to_u8(data_buff + i);
            i += 1;
            break;
        case data_save_type_int32_t:
            ele_data.data_buff._s32 = _u8_buff_transfer_to_s32(data_buff + i);
            i += 4;
            break;
        case data_save_type_int16_t:
            ele_data.data_buff._s16 = _u8_buff_transfer_to_s16(data_buff + i);
            i += 2;
            break;
        case data_save_type_int8_t:
            ele_data.data_buff._s8 = _u8_buff_transfer_to_s8(data_buff + i);
            i += 1;
            break;
        case data_save_type_float_t:
            ele_data.data_buff._f32 = _u8_buff_transfer_to_float(data_buff + i);
            i += 4;
            break;
        default:
            break;
        }
    }

    rt_mutex_release(short_cycle_data_vector_lock);

    if (data_buff) rt_free(data_buff);
    init_flag = data_save_status_ok;
    DATA_SAVE_read_short_cycle_data("long_data_addr", &now_newest_long_cycle_data_addr);

    LOG_I("data save init ok");
    return data_save_status_ok;
    end: if (data_buff) rt_free(data_buff);
    init_flag = data_save_status_fail;
    LOG_E("error: data save init fail");
    return data_save_status_fail;
}
//注册实时数据,init前应当使用该函数注册所有数据
data_save_status DATA_SAVE::DATA_SAVE_short_cycle_registe_data(std::string name, data_save_type data_type)
{
    _DATA_VECTOR newone(name, data_type);
    rt_memset(&(newone.data_buff), 0, 4);

    if (init_flag != data_save_status_not_init)
    {
        LOG_E("error:cant register data in this pos");
        return data_save_status_fail;
    }

    switch (data_type)
    {
    case data_save_type_uint32_t:
    case data_save_type_int32_t:
    case data_save_type_float_t:
        short_cycle_data_bytes_length += 4;
        break;
    case data_save_type_uint16_t:
    case data_save_type_int16_t:
        short_cycle_data_bytes_length += 2;
        break;
    case data_save_type_uint8_t:
    case data_save_type_int8_t:
        short_cycle_data_bytes_length += 1;
        break;
    default:
        break;
    }
    short_cycle_data_vector.push_back(newone);

    LOG_I("short cycle data <%s> register ok ", name.c_str());
    return data_save_status_ok;

}

//注册记录数据,init前应当使用该函数注册所有数据
data_save_status DATA_SAVE::DATA_SAVE_long_cycle_registe_data(std::string name, data_save_type data_type)
{
    _DATA_VECTOR newone(name, data_type);
    rt_memset(&(newone.data_buff), 0, 4);

    if (init_flag != data_save_status_not_init)
    {
        LOG_E("error:cant register data in this pos");
        return data_save_status_fail;
    }

    switch (data_type)
    {
    case data_save_type_uint32_t:
    case data_save_type_int32_t:
    case data_save_type_float_t:
        long_cycle_data_bytes_length += 4;
        break;
    case data_save_type_uint16_t:
    case data_save_type_int16_t:
        long_cycle_data_bytes_length += 2;
        break;
    case data_save_type_uint8_t:
    case data_save_type_int8_t:
        long_cycle_data_bytes_length += 1;
        break;
    default:
        break;
    }
    long_cycle_data_vector.push_back(newone);

    LOG_I("long cycle data <%s> register ok ", name.c_str());
    return data_save_status_ok;
}

data_save_status DATA_SAVE::DATA_SAVE_record_short_cycle_data()
{
    int i;
    uint16_t short_cycle_index_temp = 0;
    uint32_t write_addr_temp;
    uint8_t *data_buff = NULL;
    int buff_offset;
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: record should init ok first ");
        goto end;
    }

    //判断最新写入地址
    write_addr_temp = now_newest_short_cycle_data_addr + 2 * short_cycle_data_bytes_length;
    if (write_addr_temp
            >= (short_cycle_data_start_page + short_cycle_data_page_num) * eeprom->EEPROM_get_bytes_per_page()
                    - short_cycle_mines_bytes)
        write_addr_temp = short_cycle_data_start_page * eeprom->EEPROM_get_bytes_per_page();
    else
        write_addr_temp = now_newest_short_cycle_data_addr + short_cycle_data_bytes_length;

    //获取数据
    data_buff = (uint8_t *) rt_malloc(short_cycle_data_bytes_length);
    if (!data_buff)
    {
        LOG_E("error: malloc data buffer error");
        goto end;
    }
    buff_offset = 0;
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    //更新short cycle index
    if (DATA_SAVE_read_short_cycle_data("short_data_index", &short_cycle_index_temp) != data_save_status_ok)
    {
        LOG_E("error: read short_cycle_index fail, may lost some data");
    }
    short_cycle_index_temp++;
    if (DATA_SAVE_write_short_cycle_data("short_data_index", short_cycle_index_temp) != data_save_status_ok)
    {
        LOG_E("error: write short_cycle_index fail, may lost some data");
    }

    for (auto ele : short_cycle_data_vector)
    {
        switch (ele.data_type)
        {
        case data_save_type_uint32_t:
            _u32_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u32);
            buff_offset += 4;
            break;
        case data_save_type_uint16_t:
            _u16_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u16);
            buff_offset += 2;
            break;
        case data_save_type_uint8_t:
            _u8_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u8);
            buff_offset += 1;
            break;
        case data_save_type_int32_t:
            _s32_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s32);
            buff_offset += 4;
            break;
        case data_save_type_int16_t:
            _s16_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s16);
            buff_offset += 2;
            break;
        case data_save_type_int8_t:
            _s8_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s8);
            buff_offset += 1;
            break;
        case data_save_type_float_t:
            _float_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._f32);
            buff_offset += 4;
            break;
        default:
            break;
        }
    }
    rt_mutex_release(short_cycle_data_vector_lock);

    if (debug_flag)
    {
        LOG_D("printf the write eeprom data-------------");
        for (i = 0; i < short_cycle_data_bytes_length; i++)
        {
            if (i % 10 == 0 && i != 0)
            {
                rt_kprintf("\r\n");
            }
            rt_kprintf("<%X>", data_buff[i]);
        }
        rt_kprintf("\r\n");

        LOG_D("printf the write eeprom data end --------");
    }

    if (eeprom->EEPROM_write(write_addr_temp, data_buff, short_cycle_data_bytes_length) != EEPROM_status_ok)
    {
        LOG_E("error: write short cycle data to eeprom fail");
        goto end;
    }

    now_newest_short_cycle_data_addr = write_addr_temp;

    if (data_buff) rt_free(data_buff);
    if (debug_flag)
    LOG_I(" record short cycle data to eeprom success");
    return data_save_status_ok;
    end: if (data_buff) rt_free(data_buff);
    LOG_E("error: record short cycle data to eeprom fail");
    return data_save_status_fail;
}

data_save_status DATA_SAVE::DATA_SAVE_record_long_cycle_data()
{
    int i;
    uint32_t write_addr_temp = 0;
    uint8_t *data_buff = NULL;
    int buff_offset;
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: record should init ok first ");
        goto end;
    }

    //读取long cycle data写入地址
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);

    if (DATA_SAVE_read_short_cycle_data("long_data_addr", &now_newest_long_cycle_data_addr)
            != data_save_status_ok)
    {
        LOG_E("error: read long_cycle_addr fail");
    }
    rt_mutex_release(short_cycle_data_vector_lock);

    //判断最新写入地址
    write_addr_temp = now_newest_long_cycle_data_addr + 2 * long_cycle_data_bytes_length;
    if (write_addr_temp
            >= (long_cycle_data_start_page + long_cycle_data_page_num) * eeprom->EEPROM_get_bytes_per_page()
                    - short_cycle_mines_bytes)
    {
        LOG_W("warn: long cycle record is full,no more data will be record");
        goto end;
    }
    else
        write_addr_temp = now_newest_long_cycle_data_addr + long_cycle_data_bytes_length;

    //获取数据
    data_buff = (uint8_t *) rt_malloc(long_cycle_data_bytes_length);
    if (!data_buff)
    {
        LOG_E("error:create long cycle data buffer error");
        goto end;
    }
    buff_offset = 0;
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);

    for (auto ele : long_cycle_data_vector)
    {
        switch (ele.data_type)
        {
        case data_save_type_uint32_t:
            _u32_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u32);
            buff_offset += 4;
            break;
        case data_save_type_uint16_t:
            _u16_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u16);
            buff_offset += 2;
            break;
        case data_save_type_uint8_t:
            _u8_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._u8);
            buff_offset += 1;
            break;
        case data_save_type_int32_t:
            _s32_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s32);
            buff_offset += 4;
            break;
        case data_save_type_int16_t:
            _s16_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s16);
            buff_offset += 2;
            break;
        case data_save_type_int8_t:
            _s8_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._s8);
            buff_offset += 1;
            break;
        case data_save_type_float_t:
            _float_transfer_to_u8_buff((data_buff + buff_offset), ele.data_buff._f32);
            buff_offset += 4;
            break;
        default:
            break;
        }
    }
    rt_mutex_release(long_cycle_data_vector_lock);

    if (debug_flag)
    {
        LOG_D("printf the write eeprom data-------------");
        for (i = 0; i < long_cycle_data_bytes_length; i++)
        {
            if (i % 10 == 0 && i != 0)
            {
                rt_kprintf("\r\n");
            }
            rt_kprintf("<%X>", data_buff[i]);
        }
        rt_kprintf("\r\n");

        LOG_D("printf the write eeprom data end --------");
    }

    if (eeprom->EEPROM_write(write_addr_temp, data_buff, long_cycle_data_bytes_length) != EEPROM_status_ok)
    {
        LOG_E("error: write long cycle data to eeprom fail");
        goto end;
    }

    now_newest_long_cycle_data_addr = write_addr_temp;
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);

    if (DATA_SAVE_write_short_cycle_data("long_data_addr", now_newest_long_cycle_data_addr)
            != data_save_status_ok)
    {
        LOG_E("error: write long_data_addr fail, may lost some data");
    }
    rt_mutex_release(short_cycle_data_vector_lock);

    if (data_buff) rt_free(data_buff);
    if (debug_flag)
    LOG_I(" record long cycle data to eeprom success");
    return data_save_status_ok;
    end: if (data_buff) rt_free(data_buff);
    LOG_E("error: record long cycle data to eeprom fail");
    return data_save_status_fail;
}

//写入实时信息 vector中的一个值
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, uint32_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint32_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u32 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data u32 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data u32 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, uint16_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint16_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u16 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data u16 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data u16 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, uint8_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint8_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u8 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data u8 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data u8 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, int32_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int32_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s32 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data s32 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data s32 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, int16_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int16_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s16 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data s16 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data s16 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, int8_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int8_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s8 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data s8 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data s8 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_short_cycle_data(std::string name, float_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_float_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._f32 = write_data;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write short cycle data f32 <%f> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write short cycle data f32 <%f> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}

//写入周期 vector中的一个值
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, uint32_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint32_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u32 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data u32 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data u32 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, uint16_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint16_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u16 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data u16 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data u16 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, uint8_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint8_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._u8 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data u8 <%u> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data u8 <%u> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, int32_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int32_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s32 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data s32 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data s32 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, int16_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int16_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s16 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data s16 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data s16 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, int8_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int8_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._s8 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data s8 <%d> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data s8 <%d> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_write_long_cycle_data(std::string name, float_t write_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto &ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_float_t)
            {
                LOG_E("error: data name <%s> write type error", name.c_str());
                goto end;
            }
            ele.data_buff._f32 = write_data;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" write long cycle data f32 <%f> to <%s> success", write_data, name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: write long cycle data f32 <%f> to <%s> fail", write_data, name.c_str());
    return data_save_status_fail;
}

//读出实时信息 vector中的一个值
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, uint32_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }

    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint32_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            if (debug_flag)
            LOG_D("found data u32 <%u>", ele.data_buff._u32);
            *read_data = ele.data_buff._u32;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data u32 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data u32 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, uint16_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint16_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._u16;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data u16 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data u16 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, uint8_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint8_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._u8;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data u8 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data u8 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, int32_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int32_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s32;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data s32 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data s32 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, int16_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int16_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s16;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data s16 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data s16 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, int8_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int8_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s8;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data s8 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data s8 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_short_cycle_data(std::string name, float_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_float_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            if (debug_flag)
            LOG_D("found data f32 <%f>", ele.data_buff._f32);
            *read_data = ele.data_buff._f32;
            break;
        }
    }
    rt_mutex_release (short_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read short cycle data f from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read short cycle data f from <%s> fail", name.c_str());
    return data_save_status_fail;
}

//读出周期 vector中的一个值
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, uint32_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }

    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint32_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            if (debug_flag)
            LOG_D("found data u32 <%u>", ele.data_buff._u32);
            *read_data = ele.data_buff._u32;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data u32 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data u32 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, uint16_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint16_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._u16;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data u16 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data u16 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, uint8_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_uint8_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._u8;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data u8 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data u8 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, int32_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int32_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s32;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data s32 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data s32 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, int16_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int16_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s16;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data s16 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data s16 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, int8_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_int8_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            *read_data = ele.data_buff._s8;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data s8 from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data s8 from <%s> fail", name.c_str());
    return data_save_status_fail;
}
data_save_status DATA_SAVE::DATA_SAVE_read_long_cycle_data(std::string name, float_t* read_data)
{
    if (init_flag != data_save_status_ok)
    {
        LOG_E("error: should init ok first");
        goto end;
    }
    if (name.empty())
    {
        LOG_E("error: name is empty");
        goto end;
    }
    if (!read_data)
    {
        LOG_E("error: read data is null");
        goto end;
    }
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        if (ele.name == name)
        {
            if (ele.data_type != data_save_type_float_t)
            {
                LOG_E("error: data name <%s> read type error", name.c_str());
                goto end;
            }
            if (debug_flag)
            LOG_D("found data f32 <%f>", ele.data_buff._f32);
            *read_data = ele.data_buff._f32;
            break;
        }
    }
    rt_mutex_release (long_cycle_data_vector_lock);

    if (debug_flag)
    LOG_I(" read long cycle data f from <%s> success", name.c_str());
    return data_save_status_ok;
    end:
    LOG_E("error: read long cycle data f from <%s> fail", name.c_str());
    return data_save_status_fail;
}

uint8_t DATA_SAVE::_u8_buff_transfer_to_u8(uint8_t* buff)
{
    return *buff;
}

uint16_t DATA_SAVE::_u8_buff_transfer_to_u16(uint8_t* buff)
{
    uint16_t res = 0;
    res |= buff[0] << 8;
    res |= buff[1];
    return res;
}

uint32_t DATA_SAVE::_u8_buff_transfer_to_u32(uint8_t* buff)
{
    uint32_t res = 0;
    res |= buff[0] << 24;
    res |= buff[1] << 16;
    res |= buff[2] << 8;
    res |= buff[3];
    return res;
}

int8_t DATA_SAVE::_u8_buff_transfer_to_s8(uint8_t* buff)
{
    return (int8_t) (*buff);
}


int16_t DATA_SAVE::_u8_buff_transfer_to_s16(uint8_t* buff)
{
    int16_t res = 0;
    res |= buff[0] << 8;
    res |= buff[1];
    return res;
}

int32_t DATA_SAVE::_u8_buff_transfer_to_s32(uint8_t* buff)
{
    int32_t res = 0;
    res |= buff[0] << 24;
    res |= buff[1] << 16;
    res |= buff[2] << 8;
    res |= buff[3];
    return res;
}

float_t DATA_SAVE::_u8_buff_transfer_to_float(uint8_t* buff)
{
    uint32_t res = 0x0;
    res |= buff[0] << 24;
    res |= buff[1] << 16;
    res |= buff[2] << 8;
    res |= buff[3];
    return *(float_t*) &res;
}

void DATA_SAVE::_u8_transfer_to_u8_buff(uint8_t* out_buff, uint8_t u8)
{
    *out_buff = u8;
}

void DATA_SAVE::_u16_transfer_to_u8_buff(uint8_t* out_buff, uint16_t u16)
{
    *out_buff = u16 >> 8;
    *(out_buff + 1) = u16;
}

void DATA_SAVE::_u32_transfer_to_u8_buff(uint8_t* out_buff, uint32_t u32)
{
    *out_buff = u32 >> 24;
    *(out_buff + 1) = u32 >> 16;
    *(out_buff + 2) = u32 >> 8;
    *(out_buff + 3) = u32;
}

void DATA_SAVE::_s8_transfer_to_u8_buff(uint8_t* out_buff, int8_t s8)
{
    *out_buff = s8;
}

void DATA_SAVE::_s16_transfer_to_u8_buff(uint8_t* out_buff, int16_t s16)
{
    *out_buff = s16 >> 8;
    *(out_buff + 1) = s16;
}

void DATA_SAVE::_s32_transfer_to_u8_buff(uint8_t* out_buff, int32_t s32)
{
    *out_buff = s32 >> 24;
    *(out_buff + 1) = s32 >> 16;
    *(out_buff + 2) = s32 >> 8;
    *(out_buff + 3) = s32;
}

void DATA_SAVE::_float_transfer_to_u8_buff(uint8_t* out_buff, float_t f)
{
    uint32_t __f = *(uint32_t*) &f;
    *out_buff = __f >> 24;
    *(out_buff + 1) = __f >> 16;
    *(out_buff + 2) = __f >> 8;
    *(out_buff + 3) = __f;
}

data_save_status DATA_SAVE::DATA_SAVE_read_init_status()
{
    return this->init_flag;
}

void DATA_SAVE::DATA_SAVE_open_debug()
{
    this->debug_flag = 1;
}

void DATA_SAVE::DATA_SAVE_print_now_all_data()
{
    if (init_flag != data_save_status_ok) return;
    LOG_D("DATA_SAVE print data vector --------");
    LOG_D("--------short");
    rt_mutex_take(short_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : short_cycle_data_vector)
    {
        switch (ele.data_type)
        {
        case data_save_type_uint32_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u32);
            break;
        case data_save_type_uint16_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u16);
            break;
        case data_save_type_uint8_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u8);
            break;
        case data_save_type_int32_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s32);
            break;
            break;
        case data_save_type_int16_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s16);
            break;
            break;
        case data_save_type_int8_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s8);
            break;
            break;
        case data_save_type_float_t:
            LOG_D("data name <%s> value <%f>", ele.name.c_str(), ele.data_buff._f32);
            break;
            break;
        default:
            break;
        }

    }
    rt_mutex_release (short_cycle_data_vector_lock);

    LOG_D("--------long");
    rt_mutex_take(long_cycle_data_vector_lock, RT_WAITING_FOREVER);
    for (auto ele : long_cycle_data_vector)
    {
        switch (ele.data_type)
        {
        case data_save_type_uint32_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u32);
            break;
        case data_save_type_uint16_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u16);
            break;
        case data_save_type_uint8_t:
            LOG_D("data name <%s> value <%lu>", ele.name.c_str(), ele.data_buff._u8);
            break;
        case data_save_type_int32_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s32);
            break;
            break;
        case data_save_type_int16_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s16);
            break;
            break;
        case data_save_type_int8_t:
            LOG_D("data name <%s> value <%d>", ele.name.c_str(), ele.data_buff._s8);
            break;
            break;
        case data_save_type_float_t:
            LOG_D("data name <%s> value <%f>", ele.name.c_str(), ele.data_buff._f32);
            break;
            break;
        default:
            break;
        }

    }
    rt_mutex_release (long_cycle_data_vector_lock);
    LOG_D("DATA_SAVE print data vector end-----");
}
